package com.winai.launcher;

import android.content.Context;
import android.util.Log;

import androidx.annotation.Keep;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;

/**
 * Sophix入口类，专门用于初始化Sophix，不应包含任何业务逻辑。
 * 此类必须继承自SophixApplication，onCreate方法不需要实现。
 * 此类不应与项目中的其他类有任何互相调用的逻辑，必须完全做到隔离。
 * AndroidManifest中设置application为此类，而SophixEntry中设为原先Application类。
 * 注意原先Application里不需要再重复初始化Sophix，并且需要避免混淆原先Application类。
 * 如有其它自定义改造，请咨询官方后妥善处理。
 */
public class SophixStubApplication extends SophixApplication {
    public static boolean needReboot = false;//是否需要重启

    /*TODO 需更换为艾益生的账号*/
    private final String TAG = "SophixStubApplication";
    private final String AppKey = "29103185";
    private final String AppSecret = "7ea9170dbbed960f1ec37de06067f09b";
    private final String RSA = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCkqocPEEb3nY4Yc3ojHJKULccQ6S6MDoHrJk+giSueN+B/9utJxsWQ/tJoa8/82+ZzhtYF8pOFfhzrHYzdzncWTRTdzIAbRJOAr7O9n8aMsDfS2HWM+MTYeSYtxr3F8Zoe1n7WB+B/UntZ8m7TGTjP25C5w20ZsUG8+5y9fQ3E41d+CitoskKQCpYiXapuJyUxjfaSwOO7ZoeH/ktKUN7jvK7pr/zlB17ZrJbXiBFzaf3FibAJn/MJBVPPfU3hA923m1mGlgcqyABRl26YP3JOBfNmh16g3qJFbXhvTiO3SkSt+UoyrkPDhlQu3S98vvhiIc/h7kxhBpDOISA2tbkzAgMBAAECggEADrqKFhjYoDFhkmJR3G4j1kk58xNIbJKnoEbu/GcIlcBdvlRxu0miRlPksh5injV1KYQ4BJQsCda783PTM8vR7xa59RTjqIy7vNj6dM2jztW8SInACfn+cXZ0qceLFfeEV5nMAoz10a2pBuSecRdic4UqLtOCILgwLx06aeCTqLagGu5xCWSLwwTCbIuJNp/Jrt/s6V8Wvr4irgg0HMxgxdUxIEtqGw4kW3VPuAqUd6D8xOcZIWFlUhthipEPjIk3q8TLbliRGbqeTv66jt/qvo8pj3uuts5Lnzjb2ELTrsuUcsW/PzGJ3dLCtsT57G+wna1xaPMF0uNrUiPD/fkLEQKBgQDlN0nVuNpN3F8yauP9wTzb2fl/Nl7pwd3pC34Y+Q+2XYv/iy7ZYvEIyBKwCisPpkTGBRRYLi3srqO5VFP7qabPKvybwzAgzqHFH2qcMPih6ZDFvNuHdPaLAI3rVtwng3qlBAswVipWlh8TmxihmkotADCM0JThahsRwu21A76PhQKBgQC36E99b1Df2JesrYUge/W3A9X7c/cQhDHTfuhjtZbcFy4F3EDsuV0yRqHiubLBPxA8/0Nhyxblcl6mL+74tUcJnXTwdZOEQwHO0jphI027D2P3e4ASzg6XEl5TUDpDZxz0Xs6oMCellamb3s7q7gCVWGtn/xNsR07LkyUtHDYXVwKBgAqDdLGG3wx/NSac3IRBHsd+wgDhv4y9Ra5wxoHiuqPEuq0n9yglb6TVeHX2oW8AU1s51GsPAzA9+mh/w7JhX7NU521FkKmQkL735+3uGRlkGy3WscMOzFgn2FqZC1QMpc132AOJkDuUeyxtCr4u04Qo5BExaALG2SMBa+HHoC/5AoGBAJLTq1sG23fLsdSQOKAlt5glPNrjy9Xo5OrpAFpS6R3Qd9L/9OnKWCjCkY61GY0Fb05zsfVKsn3/YKenz0/LUurPRkSQanUHFlNsLmXNTMrSz8oPVMIutyFP7fTwMYQMt+Y2JyHKSGz/3wjtyx9vYxHZJO9Di9s/1x3a3ax1QI5DAoGAb9y60IZh7MSLJAG4fP4zCC3IUytKpX4rmpjcNr3K6E4K55FItMmOFvaq8sVc9ElOHd20FFD6Jmp+iZ4aBe2Sy1qbW6fd63kt4VO9UQMrAI8zHMScI/Bh6bn6MLlxA1Gw5RjwgEfpb1Vd1IAVKodUmSwJWH9oslsH3PTMwSWpCdQ=";

    // 此处SophixEntry应指定真正的Application，并且保证RealApplicationStub类名不被混淆。
    @Keep
    @SophixEntry(Application.class)
    static class RealApplicationStub {
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        initSophix();
    }

    private void initSophix() {
        String appVersion = "0.0.0";
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setAppVersion(appVersion)
                .setSecretMetaData(AppKey, AppSecret, RSA)
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub((mode, code, info, handlePatchVersion) -> {
                    if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                        Log.i(TAG, "sophix load patch success!");
                    } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                        // 如果需要在后台重启，建议此处用SharePreference保存状态。
                        Log.i(TAG, "sophix preload patch success. restart app to make effect.");
                        needReboot = true;
                    }
                }).initialize();
        instance.cleanPatches();//TODO 清空本地补丁,正式发布时需隐藏
    }
}
